package com.sunlg.smarthome.ui.plant;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseFragment;
import com.sunlg.smarthome.contract.PumpContract;
import com.sunlg.smarthome.contract.presenter.PumpPresenter;
import com.sunlg.smarthome.data.entity.Pump;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class PumpListFragment extends BaseFragment
        implements PumpContract.View, PumpAdapter.OnItemClickListener{


    RecyclerView rvPumpList;

    private PumpAdapter pumpAdapter;
    private List<Pump> pumpList;
    private Context mContext;

    protected PumpContract.Presenter presenter;

    public PumpListFragment() {

    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_pump;
    }


    @Override
    protected void init() {
        presenter = new PumpPresenter();
        presenter.attachView(this);

        mContext = getActivity();
        pumpList = new ArrayList<>();
        pumpAdapter = new PumpAdapter(mContext, pumpList);
        pumpAdapter.setOnItemClicListener(this);
    }

    @Override
    protected void initView(View view) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        rvPumpList = (RecyclerView) view.findViewById(R.id.rv_pump_list);
        rvPumpList.setLayoutManager(layoutManager);
        rvPumpList.setAdapter(pumpAdapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        presenter.loadAllPump();
    }

    @Override
    public void onItemClick(View view, int postion) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("pump", pumpList.get(postion));
        overlay(PumpActivity.class, bundle);
    }

    @Override
    public void showNoneDeviceText() {

    }

    @Override
    public void hideNoneDeviceText() {

    }

    @Override
    public void showPumpList(List<Pump> deviceList) {
        pumpList.clear();
        pumpList.addAll(deviceList);
        pumpAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.dettachView();
    }
}
